package personal.gavin.gtweet.widget
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import personal.gavin.gtweet.event.*;
	
	public class URLRequestHelp
	{
		public function URLRequestHelp(url:String)
		{
			client = new URLLoader();           	
            configureListeners(client);
            var random:int = (int)(Math.random()*100000);
            request = new URLRequest(url + "&count=" + random);
            
		}
		private var client:URLLoader;
		private var status:int = -1;
		private var request:URLRequest;
		
		/**
		 * begin to load data according to configuration 
		 */		
		public function load():void {
			try {
                client.load(request);
            } catch (error:Error) {
                trace("Unable to load requested document.");
            }
		}
		
		/**
		 * events dispatcher 
		 */		
		public var _eventDispatcher:EventDispatcher=new EventDispatcher();
		
		/**
		 * configure how to dispatch event
		 * @param dispatcher loader
		 */		
		private function configureListeners(dispatcher:IEventDispatcher):void {
			dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            dispatcher.addEventListener(Event.OPEN, openHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        }
        
        private function completeHandler(event:Event):void {
            var loader:URLLoader = URLLoader(event.target);
            if( this.status == 200){
				var stringData:String = loader.data;
				var evt:LoadFinishEvent;
		    	evt = new LoadFinishEvent(LoadFinishEvent.LOADFINISHED,stringData);
		    	_eventDispatcher.dispatchEvent(evt);
			}
            //trace("completeHandler: " + loader.data);
        }

        private function openHandler(event:Event):void {
            //trace("openHandler: " + event);
        }

        private function progressHandler(event:ProgressEvent):void {
            //trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
        }

        private function securityErrorHandler(event:SecurityErrorEvent):void {
            //trace("securityErrorHandler: " + event);
        }

        private function httpStatusHandler(event:HTTPStatusEvent):void {
            trace("httpStatusHandler: " + event);
            this.status = event.status;
            if(event.status == 0){
            	//in firefox is 0
            	this.status = 200;
            }
            if (this.status != 200) {
        		var evt:LoadFinishEvent;
            	evt = new LoadFinishEvent(LoadFinishEvent.LOADFINISHED,null,event.status);
            	_eventDispatcher.dispatchEvent(evt);
        	}
        }

        private function ioErrorHandler(event:IOErrorEvent):void {
            //trace("ioErrorHandler: " + event);
        }
	}
}